from mg_app_framework import MesCode, get_logger
from tornado_request_mapping import request_mapping

from common_bind_service.common.base.base_handler import BaseHandler
from common_bind_service.models.param.container_param import CheckContainerUsefulParam, ContainerRemoveProdParam, \
    ContainerBindParam, ContainerUnBindParam, ContainerListParam
from common_bind_service.pkg import auth
from common_bind_service.pkg.log.log_decorator import log_decorator
from common_bind_service.service.upperpc_service import UpperPcService


@request_mapping('/vehicle_mgm')
class UpperPcContainerHandler(BaseHandler):
    """上位机进行调用的配方接口"""

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)
        self.upperpc_service = UpperPcService()

    @request_mapping('/vehicle_check', 'post', 'F050 上位机调用: 校验载具是否为空的可用状态')
    @log_decorator()
    async def vehicle_check(self):
        data = self.data
        param = self.check_params(CheckContainerUsefulParam, **data)
        status, err = auth.check_upperpc_callid(param.upperpc_code, param.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.upperpc_service.vehicle_check(param)
        return self.send_response_data(response.code, response.data, response.msg)

    @request_mapping('/vehicle_bind', 'post', 'F051 上位机调用: 给容器组盘')
    @log_decorator()
    async def vehicle_bind(self):
        """将电芯码组合，和载具进行绑定，组合时MES需校验电芯码状态和托盘内电芯码重复校验"""
        data = self.data
        param = self.check_params(ContainerBindParam, **data)
        status, err = auth.check_upperpc_callid(param.upperpc_code, param.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.upperpc_service.vehicle_bind(param)
        return self.send_response_data(response.code, response.data, response.msg)

    @request_mapping('/vehicle_unbind', 'post', 'F052 上位机调用: 解除绑定关系')
    @log_decorator()
    async def vehicle_unbind(self):
        """解除电芯与载具之间的绑定关系，解绑前先校验上位机传过来的托盘码是否已出站状态，并且载具不为空"""
        data = self.data
        param = self.check_params(ContainerUnBindParam, **data)
        status, err = auth.check_upperpc_callid(param.upperpc_code, param.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.upperpc_service.vehicle_unbind(param)
        return self.send_response_data(response.code, response.data, response.msg)

    @request_mapping('/vehicle_ng_out', 'post', '上位机调用: 剔除ng电芯码')
    @log_decorator()
    async def vehicle_ng_out(self):
        """载具中存在被判定为NG的电芯，从载具中剔除，解除电芯码与载具码的绑定关系"""
        data = self.data
        param = self.check_params(ContainerRemoveProdParam, **data)
        status, err = auth.check_upperpc_callid(param.upperpc_code, param.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.upperpc_service.vehicle_ng_out(param)
        return self.send_response_data(response.code, response.data, response.msg)

    @request_mapping('/vehicle_cell_list', 'post', '上位机调用: 获取容器内电芯')
    @log_decorator()
    async def vehicle_cell_list(self):
        """获取载具内的电芯码信息"""
        data = self.data
        param = self.check_params(ContainerListParam, **data)
        status, err = auth.check_upperpc_callid(param.upperpc_code, param.call_id)
        if not status:
            return self.send_response_data(MesCode.fail, None, err)
        response = await self.upperpc_service.vehicle_cell_list(param)
        return self.send_response_data(response.code, response.data, response.msg)






